﻿using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Text;

namespace IPTV_API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class IPTVApiController:ControllerBase
    {
        private readonly ILogger<IPTVApiController> _logger;
        public IPTVApiController(ILogger<IPTVApiController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public string Get(string? aaa,string type="m3u")
        {
            var request = HttpContext.Request;
            string requrl = $"{request.Scheme}://{request.Host}";
            Dictionary<string, string> zjurls = new Dictionary<string, string>() 
            {
                {"浙江卫视",$"{requrl}/Channel/Channel.m3u8?id=zjws" },
                {"钱江都市",$"{requrl}/Channel/Channel.m3u8?id=zjqj" },
                {"浙江经济生活",$"{requrl}/Channel/Channel.m3u8?id=zjjjsh" },
                {"浙江教科影视",$"{requrl}/Channel/Channel.m3u8?id=zjjkys" },
                //{"浙江影视娱乐",$"{requrl}/Channel/Channel.m3u8?id=zjysyl" },
                {"浙江民生休闲",$"{requrl}/Channel/Channel.m3u8?id=zjmsxx" },
                {"浙江新闻",$"{requrl}/Channel/Channel.m3u8?id=zjxw" },
                {"浙江少儿",$"{requrl}/Channel/Channel.m3u8?id=zjse" },
                //{"浙江留学",$"{requrl}/Channel/Channel.m3u8?id=zjlx" },
                {"浙江国际",$"{requrl}/Channel/Channel.m3u8?id=zjgj" },
                {"浙江好易购",$"{requrl}/Channel/Channel.m3u8?id=zjhyg" },
                {"浙江之江纪录",$"{requrl}/Channel/Channel.m3u8?id=zjzjjl" },
                {"绍兴新闻综合",$"http://live.shaoxing.com.cn/video/s10001-sxhb/index.m3u8" },
                {"绍兴文化影视",$"http://live.shaoxing.com.cn/video/s10001-sxtv3/index.m3u8" },
                {"绍兴公共频道",$"http://live.shaoxing.com.cn/video/s10001-sxtv2/index.m3u8" },
            };

            if (type=="txt")
            {
                //string content = $"vv" +
                //    $"浙江卫视,{requrl}/Channel?id=zjws\r\n" +
                //    $"钱江都市,{requrl}/Channel?id=zjqj\r\n" +
                //    $"浙江经济生活,{requrl}/Channel?id=zjjjsh\r\n" +
                //    $"浙江教科影视,{requrl}/Channel?id=zjjkys\r\n" +
                //    $"浙江影视娱乐,{requrl}/Channel?id=zjysyl\r\n" +
                //    $"浙江民生休闲,{requrl}/Channel?id=zjmsxx\r\n" +
                //    //$"浙江新闻,{requrl}/Channel?id=zjxw\r\n" +
                //    $"浙江少儿,{requrl}/Channel?id=zjse\r\n" +
                //    $"浙江留学,{requrl}/Channel?id=zjlx\r\n" +
                //    //$"浙江国际,{requrl}/Channel?id=zjgj\r\n" +
                //    $"浙江好易购,{requrl}/Channel?id=zjhyg\r\n" +
                //    $"浙江之江纪录,{requrl}/Channel?id=zjzjjl\r\n" +
                //    $"绍兴新闻综合,http://live.shaoxing.com.cn/video/s10001-sxhb/index.m3u8\r\n" +
                //    $"绍兴文化影视,http://live.shaoxing.com.cn/video/s10001-sxtv3/index.m3u8\r\n" +
                //    $"绍兴公共频道,http://live.shaoxing.com.cn/video/s10001-sxtv2/index.m3u8\r\n";
                string content = "浙江频道,#genre#\r\n";
                foreach (var item in zjurls)
                {
                    content += $"{item.Key},{item.Value}\r\n";
                }
                string url = "http://192.168.3.61:5000/Sub?type=txt";
                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        string res = client.GetStringAsync(url).Result;
                        content += res;
                    }
                }
                catch (Exception)
                {
                }
                return content;
            }
            else
            {
                string content = "";
                //string url = "http://192.168.3.61:5000/Sub?type=m3u";
                //try
                //{
                //    using (HttpClient client = new HttpClient())
                //    {
                //        string res = client.GetStringAsync(url).Result;
                //        content += res;
                //    }
                //}
                //catch (Exception)
                //{
                //}
                if (string.IsNullOrEmpty(content))
                {
                    content = "#EXTM3U x-tvg-url=\"https://epg.doube.eu.org/epg.xml\" atchup=\"append\" catchup-source=\"?playseek=${(b)yyyyMMddHHmmss}-${(e)yyyyMMddHHmmss}\"\r\n";
                }
                foreach (var item in zjurls)
                {
                    content += $"#EXTINF:-1 group-title=\"浙江频道\",{item.Key}\r\n{item.Value}\r\n";
                }
                return content;
            }
        }
        
    }
}
